package com.ymatou.shop.ui.view.item;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ymatou.shop.R;
import com.ymatou.shop.cache.ImageCache;
import com.ymatou.shop.model.Message;
import com.ymatou.shop.util.DateUtil;
import com.ymatou.shop.util.StringUtil;
import com.ymatou.shop.util.Utils;

public class MessageItemView extends FrameLayout {
	
	private ImageView mHeaderImageView;
	private TextView mNameTextView;
	private TextView mDateTextView;
	private TextView mContentTextView;
	
	private View mReplyRootView;
	private ImageView mReplyImageView;
	private TextView mReplyDateTextView;
	private TextView mReplyContentTextView;
	
	private Message mItem;

	public MessageItemView(Context context) {
		super(context);
		initMessageItemView();
	}
	
	private void initMessageItemView() {
		LayoutInflater.from(getContext()).inflate(R.layout.list_message_item, this);
		
		mHeaderImageView = (ImageView) findViewById(R.id.header_image);
		mNameTextView = (TextView) findViewById(R.id.name_txt);
		mDateTextView = (TextView) findViewById(R.id.date_txt);
		mContentTextView = (TextView) findViewById(R.id.content_txt);
		
		mReplyRootView = findViewById(R.id.reply_root);
		mReplyImageView = (ImageView) findViewById(R.id.reply_image);
		mReplyDateTextView = (TextView) findViewById(R.id.reply_date_txt);
		mReplyContentTextView = (TextView) findViewById(R.id.reply_content_txt);
	}
	
	public void setData(Message item) {
		mItem = item;
		
		mHeaderImageView.setTag(mItem.mReplyInfo.mUserHeaderUrl);
		Drawable drawable = ImageCache.get(getContext(), mItem.mReplyInfo.mUserHeaderUrl, new ImageCache.OnIconLoadCompleteListener() {
			@Override
			public void onLoadComplete(String url, boolean success, Drawable drawable) {
				if (StringUtil.equals(url, (String) mHeaderImageView.getTag())) {
					mHeaderImageView.setImageDrawable(drawable);
				}
			}
		});
		if (drawable == null) {
			drawable = getResources().getDrawable(R.drawable.ic_launcher);
		}
		mHeaderImageView.setImageDrawable(drawable);
		mNameTextView.setText(mItem.mReplyInfo.mUserName);
		mDateTextView.setText(Utils.formatDate(mItem.mReplyInfo.mTime));
		mContentTextView.setText(mItem.mReplyInfo.mContent);
		
		if (mItem.mReplyToInfo != null) {
			mReplyRootView.setVisibility(View.VISIBLE);
			mReplyContentTextView.setText(mItem.mReplyToInfo.mContent);
			mReplyDateTextView.setText(DateUtil.format(mItem.mReplyToInfo.mTime, "MM-dd HH:mm"));
		} else {
			mReplyRootView.setVisibility(View.GONE);
		}
	}

}
